﻿using System;
using System.Windows.Forms;
using Autodesk.Revit.UI;

namespace YArchitech.Temp
{
	internal class Helper
	{
		public IntPtr RevitHandle
		{
			get
			{
				return this.m_revitHandle;
			}
			set
			{
				this.m_revitHandle = value;
			}
		}

		public ExternalCommandData CommandData { get; set; }

		public bool SearchBranch()
		{
			Search search = new Search();
			return search.Run(this.CommandData.Application.ActiveUIDocument) && search.DumpIntoDuctSystem(out this.m_system);
		}

		public void ShowMainForm(bool bFirst = true)
		{
			if (this.m_form == null)
			{
				this.m_form = new MainForm(this.CommandData.Application.ActiveUIDocument);
			}
			if (this.m_system != null && bFirst)
			{
				this.m_form.Attach(this.m_system);
				bFirst = false;
			}
			this.m_form.WindowState = FormWindowState.Normal;
			DialogResult dialogResult = this.m_form.ShowDialog();
			if (dialogResult == DialogResult.OK)
			{
				if (this.SearchBranch())
				{
					this.m_form.Attach(this.m_system);
					bFirst = false;
				}
				this.ShowMainForm(bFirst);
				return;
			}
			if (dialogResult != DialogResult.Abort)
			{
				return;
			}
			if (new Search().BeMinimizeOperator(this.CommandData.Application.ActiveUIDocument, this.m_form.ElemIds))
			{
				this.m_form.Attach(this.m_system);
			}
			this.ShowMainForm(false);
		}

		private MainForm m_form;

		private HYDuctSystem m_system;

		private IntPtr m_revitHandle = IntPtr.Zero;
	}
}
